package com.dlyk.web;

import com.dlyk.model.TActivity;
import com.dlyk.query.ActivityQuery;
import com.dlyk.result.R;
import com.dlyk.service.ActivityService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/18 16:40:03
 * @version: 1.0
 */
@RestController
public class ActivityController {

    @Resource
    private ActivityService activityService;

    /**
     * @description: 用户列表分页查询
     * @params: [current]
     * @return:
     * @author: ajie
     * @date: 2024/7/13 23:14
     */
    @GetMapping("api/activitys")
    public R activityPage(@RequestParam(value = "current",required = false) Integer current, ActivityQuery activityQuery){
        if (current == null){
            current = 1;
        }
        PageInfo<TActivity> pageInfo = activityService.getActivityByPage(current,activityQuery);
        return R.OK(pageInfo);
    }

    /**
     * @description: 添加市场活动
     * @params: [activityQuery, token]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 16:39
     */
    @PostMapping("api/activity")
    public R addActivity(ActivityQuery activityQuery, @RequestHeader(value = "Authorization") String token){
        activityQuery.setToken(token);
        int save = activityService.saveActivity(activityQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 查询市场活动
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 16:38
     */
    @GetMapping("api/activity/{id}")
    public R loadActivity(@PathVariable(value = "id") Integer id){
        TActivity activity = activityService.getActivityById(id);
        return R.OK(activity);
    }

    /**
     * @description: 编辑市场活动
     * @params: [activityQuery, token]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 16:38
     */
    @PutMapping("api/activity")
    public R editActivity(ActivityQuery activityQuery, @RequestHeader(value = "Authorization") String token){
        activityQuery.setToken(token);
        int update = activityService.updateActivity(activityQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 删除市场活动
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 16:38
     */
    @DeleteMapping("api/activity/{id}")
    public R delUser(@PathVariable(value = "id") Integer id){
        int del = activityService.delActivityById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 批量删除市场活动
     * @params: [ids]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 16:51
     */
    @DeleteMapping("api/activity")
    public R batchDelUser(@RequestParam(value = "ids") String ids){
        //ids = "1,3,5,7";
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = activityService.batchDelActivityByIds(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }

    /**
     * 查询所有的市场活动
     *
     * @return
     */
    @GetMapping(value = "/api/activity/all")
    public R allActivity() {
        List<TActivity> tActivityList = activityService.getActivityByAll();
        return R.OK(tActivityList);
    }
}
